/*** In The Name of Allah ***/
package com.codeanalysissys.backend.graphs.ast;

import com.codeanalysissys.backend.cpp.CppASTBuilder;
import com.codeanalysissys.backend.cpp.CppCFGBuilder;
import com.codeanalysissys.backend.golang.GoASTBuilder;
import com.codeanalysissys.backend.golang.GoCFGBuilder;
import com.codeanalysissys.backend.graphs.cfg.ControlFlowGraph;
import com.codeanalysissys.backend.java.JavaASTBuilder;
import com.codeanalysissys.backend.java.JavaCFGBuilder;
import com.codeanalysissys.backend.javascript.JavaScriptASTBuilder;
import com.codeanalysissys.backend.javascript.JavaScriptCFGBuilder;
import com.codeanalysissys.backend.python.PythonASTBuilder;
import com.codeanalysissys.backend.python.PythonCFGBuilder;
import com.codeanalysissys.backend.ruby.RubyASTBuilder;
import com.codeanalysissys.backend.ruby.RubyCFGBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * Abstract Syntax Tree (AST) Builder.
 * This class invokes the appropriate builder based on the given language parameter.
 *
 * @author Seyed Mohammad Ghaffarian
 */
public class ASTBuilder {

    /**
     * Build and return the CFG of the given source code file with specified language.
     */
    public static AbstractSyntaxTree build(String lang, String srcFilePath) throws IOException {
        switch (lang) {
            case "Cpp":
                return CppASTBuilder.build(srcFilePath);
            //
            case "C":
                return null;
            //
            case "Java":
                return JavaASTBuilder.build(srcFilePath);
            //
            case "Python":
                return PythonASTBuilder.build(srcFilePath);
            //
            case "Ruby":
                return RubyASTBuilder.build(srcFilePath);
            //
            case "JavaScript":
                return JavaScriptASTBuilder.build(srcFilePath);
            //
            case "Golang":
                return GoASTBuilder.build(srcFilePath);
            //
            default:
                return null;
        }
    }

    /**
     * Build and return the CFG of the given source code file with specified language.
     */
    public static AbstractSyntaxTree build(String lang, String fileName, InputStream inputStream) throws IOException {
        switch (lang) {
            case "Cpp":
                return CppASTBuilder.build(fileName, inputStream);
            //
            case "C":
                return null;
            //
            case "Java":
                return JavaASTBuilder.build(fileName, inputStream);
            //
            case "Python":
                return null;
            case "Ruby":
                return RubyASTBuilder.build(fileName, inputStream);
            //
            case "JavaScript":
                return JavaScriptASTBuilder.build(fileName, inputStream);
            //
            case "Golang":
                return GoASTBuilder.build(fileName, inputStream);
            //
            default:
                return null;
        }
    }

    /**
     * Build and return the CFG of the given content with specified language.
     */
    public static AbstractSyntaxTree build(String lang, InputStream content) throws IOException {
        switch (lang) {
            case "Cpp":
                return CppASTBuilder.build(content);
            //
            case "C":
                return null;
            //
            case "Java":
                return JavaASTBuilder.build(content);
            //
            case "Python":
                return PythonASTBuilder.build(content);
            //
            case "Ruby":
                return RubyASTBuilder.build(content);
            //
            case "JavaScript":
                return JavaScriptASTBuilder.build(content);
            //
            case "Golang":
                return GoASTBuilder.build(content);
            //
            default:
                return null;
        }
    }
}
